package cn.kgc.service.impl;

import cn.kgc.constants.Constants;
import cn.kgc.mapper.SmsLogMapper;
import cn.kgc.pojo.SmsLog;
import cn.kgc.service.SmsLogService;
import cn.kgc.utils.redis.RedisUtils;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.teaopenapi.models.Config;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 * 短信记录 服务实现类
 * </p>
 *
 * @author qf
 * @since 2022-03-03
 */
@Service
public class SmsLogServiceImpl extends ServiceImpl<SmsLogMapper, SmsLog> implements SmsLogService {

    private final RedisUtils redisUtils;

    private final SmsLogMapper smsLogMapper;

    public SmsLogServiceImpl(RedisUtils redisUtils, SmsLogMapper smsLogMapper) {
        this.redisUtils = redisUtils;
        this.smsLogMapper = smsLogMapper;
    }

    //除了发送短信之外，还需要在数据库中存在发送的日志
    @Override
    public Map<String, String> sendSms(String phone, String codeType) {

        Map<String,String> map=new HashMap<>();
        //默认发送失败
        Integer status = Constants.Sms.STATUS_SEND_FAILED;
        //调用阿里的api

        //1.生成验证码
//        Integer random = (int)(Math.random()*9000+1000);
//        String sendCode = random.toString();
       String sendCode="0000";
//
//        Client client = null;
//
//
//        try {
//            client=createClient (
//                          "LTAI5tLUCcupg3RLcLYgXaXw",
//                          "bKPsj9b7qKRVKLIV92pllIO2eUD6TQ");
//        } catch (Exception e) {
//            e.printStackTrace ();
//        }
//
//        SendSmsRequest sendSmsRequest=new SendSmsRequest ()
//                .setSignName ("阿里云短信测试")
//                .setTemplateCode ("SMS_154950909")
//                .setPhoneNumbers (phone)
//                .setTemplateParam ("{\"code\":\""+sendCode+"\"}");
//
//        String templateParam = sendSmsRequest.getTemplateParam ();
//
//        SendSmsResponse sendSmsResponse =null;
//
//        try {
//            sendSmsResponse = client.sendSms (sendSmsRequest);
//        } catch (Exception e) {
//            e.printStackTrace ();
//        }
//
//        String msgResult = sendSmsResponse.getBody ().getMessage ();
//
//
       String msgResult="OK";
//        //短信发送成功
//
//        if (msgResult !=null && msgResult.equals("OK")){
//            status=Constants.Sms.STATUS_SEND_SUCCESS;
//            //拼接前缀
//            String key= RedisKeyUtils.formatKeyWithPrefix(Constants.Redis.PREFIX_SMS,codeType,phone);
//            //2.存到redis
//            redisUtils.putValue(key,sendCode,Constants.Duration.MINUTE);
//        }

        SmsLog smsLog = new SmsLog();
        smsLog.setCodeType(Integer.parseInt(codeType));
        smsLog.setPhone(phone);
        smsLog.setMessage(sendCode);
        smsLog.setStatus(status);
        smsLog.setFailInfo(msgResult);
        this.save(smsLog);
        System.out.println ("手机号="+phone);
        return map;
    }
    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new Client(config);
    }
}
